﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace SpriteSample
{
    class Player : Object
    {

        /// <summary>
        /// Cac Sprite can co: Stand, Walk, Jump, Attack, Hit, Die
        /// </summary>
        public Sprite m_sStanding;
        public Sprite m_sWalking;
        public Sprite m_sJumping;

        /// <summary>
        /// Khoi tao 1 Object Player: 
        /// Huong ve ben trai
        /// Phan loai Object la Player
        /// Gia toc la 0.9 (toc do di chuyen) va 0.2 (trong luc)
        /// </summary>
        /// <param name="_posX"></param>
        /// <param name="_posY"></param>
        public Player(float _posX, float _posY)
            : base(_posX, _posY)
        {
            m_eDirFace = eDirFace.Left;
            m_eSpecies = eSpecies.Player;
            m_eStatus = eStatus.Jumping;
            m_vAccloc = new Vector2(0.9f, 5.0f);
        }

        /// <summary>
        /// tao va load Sprite can thiet cho player
        /// gan m_curSprite (Sprite ban dau) cho 1 sprite mac dinh
        /// va set gia tri Size cho Player (de xet va cham)
        /// </summary>
        /// <param name="_contentManager"></param>
        public override void LoadContent(ContentManager _contentManager)
        {
            m_sStanding = new Sprite(ResourceManager.GetInstance().GetSprite("Player/queen_standing"));
            
            m_sWalking = new Sprite(ResourceManager.GetInstance().GetSprite("Player/queen_walking"));
            m_sJumping = new Sprite(ResourceManager.GetInstance().GetSprite("Player/queen_jump"));
            //m_currSprite = m_sWalking;
            //m_vSize.X = m_currSprite.Width;
            //m_vSize.Y = m_currSprite.Height;

        }

        float _jumpHeight = 0;
        /// <summary>
        /// Chi thay doi Van toc dua tren Gia toc khong doi gia tri, chi doi huong
        /// Van toc = Gia toc; Vi tri += Van toc
        /// Vi tri cua Sprite = Vi tri Player
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            m_keyInput.Update();

            if (m_keyInput.IsDown(Keys.Left))
            {
                if (m_eStatus != eStatus.Jumping)
                {
                    m_eStatus = eStatus.Walking;
                }
                m_eDirFace = eDirFace.Left;
                if (m_vAccloc.X > 0)
                    m_vAccloc.X *= -1;
                m_vPos.X += m_vVecloc.X;
            }
            if (m_keyInput.IsDown(Keys.Right))
            {
                if (m_eStatus != eStatus.Jumping)
                {
                    m_eStatus = eStatus.Walking;
                }
                m_eDirFace = eDirFace.Right;
                if (m_vAccloc.X < 0)
                    m_vAccloc.X *= -1;
                m_vPos.X += m_vVecloc.X;
            }

            if (m_keyInput.IsReleased(Keys.Right) || m_keyInput.IsReleased(Keys.Left))
            {
                /// khi con jumping (dang tren khong, thi khong chuyen thanh stand
                /// nguoc lai thi chuyen status -> standing
                if (m_eStatus != eStatus.Jumping)
                {
                    m_eStatus = eStatus.Standing;
                }
            }

            /// toi do cao max, Gia toc > 0 (de roi xuong)
            if (_jumpHeight < -100)
            {
                if (m_vAccloc.Y < 0)
                    m_vAccloc.Y *= -1;
                    //m_vAccloc.Y = Math.Abs(m_vAccloc.Y);
            }

            if (m_keyInput.IsPressed(Keys.Up))
            {
                if (_jumpHeight == 0)
                {
                    m_eStatus = eStatus.Jumping;
                    //if (m_vAccloc.Y > 0)
                    //    m_vAccloc.Y *= -1;
                    //m_vAccloc.Y = -2.0f; ////
                    m_vAccloc.Y = -Math.Abs(m_vAccloc.Y);
                    _jumpHeight = 0;
                }
            }
            //_jumpHeight += m_vAccloc.Y;

            /// chon sprite phu hop voi status
            switch (m_eStatus)
            {
                case eStatus.Standing:
                    m_currSprite = m_sStanding;
                    _jumpHeight = 0;
                    //m_vAccloc.Y = 0;
                    break;
                case eStatus.Walking:
                    m_currSprite = m_sWalking;
                    //m_vAccloc.Y = 0;
                    //m_vPos.Y += m_vVecloc.Y;
                    break;
                case eStatus.Jumping:
                    m_currSprite = m_sJumping;
                    m_vPos.Y += m_vVecloc.Y;
                    _jumpHeight += m_vAccloc.Y;
                    break;
                default:
                    break;
            }
            
            /// flip sprite phu hop direction
            if (m_eDirFace == eDirFace.Right)
            {
                m_currSprite.Effect = SpriteEffects.FlipHorizontally;
            }
            else
            {
                m_currSprite.Effect = SpriteEffects.None;
            }

            m_vVecloc = m_vAccloc;
            /// dong bo vi tri cua player va Sprite
            m_currSprite.Position = m_vPos;
            m_currSprite.Update(gameTime);
        }

        public override void UpdateCollision(List<Object> _listObject)
        {
            bool isFalling = true;
            for (int i = 0; i < _listObject.Count(); i++)
            {
                if (this.Bound.Intersects(_listObject[i].Bound) && _listObject[i].m_eSpecies != eSpecies.Player)
                {
                    isFalling = false;
                    if (_listObject[i].m_eSpecies == eSpecies.TitleObject)//check va cham voi vat la Title object
                    {
                        eDirConllision result = CheckCollision(this, _listObject[i]);
                        if (result == eDirConllision.None)
                        {
                            
                        }
                        if (result == eDirConllision.Down) //player stand on obj2
                        {
                            /// neu vat dang roi xuong Accloc duong --> chuyen thanh am (da tiep dat) va chuyen trang thai Stand

                            if (m_vAccloc.Y > 0)
                            {
                                //m_vAccloc.Y *= -1;

                                if (m_eStatus == eStatus.Jumping)
                                {
                                    m_eStatus = eStatus.Standing;
                                    _jumpHeight = 0;
                                    //m_vAccloc.Y = 0;
                                }

                            }
                        }
                        if (result == eDirConllision.Right)
                        {
                            //m_vPos.X -= 0.91f;
                            m_vPos.X -= (Math.Abs(m_vAccloc.X) + 0.01f);
                        }
                        if (result == eDirConllision.Left)
                        {
                            //m_vAccloc.X = 0;
                            //m_vPos.X += 0.91f;
                            m_vPos.X += (Math.Abs(m_vAccloc.X) + 0.01f);
                        }
                        if (result == eDirConllision.Up)
                        {

                        }

                    }
                }
                
            }
            if (isFalling == true && m_eStatus == eStatus.Walking)
            ////if (_isFalling == true && m_eStatus != eStatus.Jumping)
            {
                m_eStatus = eStatus.Jumping;
                m_vAccloc.Y = Math.Abs(m_vAccloc.Y);
            }
        }
        public override void Render(GameTime gameTime, SpriteBatch _spriteBatch)
        {
            m_currSprite.Draw(_spriteBatch);
        }
    }
}
